package com.hongsheng.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hongsheng.pojo.Driver;
import com.hongsheng.pojo.Goods;
import com.hongsheng.pojo.Line;
import com.hongsheng.pojo.Order;
import com.hongsheng.util.DriverSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * ClassName:DirverMapper
 * Description:
 *
 * @Author: 曾晋冠
 * @Version: 2022.3.2
 * @Create: 2023/8/10 - 16:40
 */
@Mapper
public interface DriverMapper extends BaseMapper<Driver> {
    /**
     * 查询司机和线路
     * @return
     */
    @Select("select * from tb_driver")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "driverName",column = "driver_name"),
            @Result(property = "driverPhone",column = "driver_phone"),
            @Result(property = "type",column = "type"),
            @Result(property = "carLoad",column = "car_load"),
            @Result(property = "size",column = "size"),
            @Result(property = "salary",column = "salary"),
            @Result(property = "status",column = "status"),
            @Result(property = "address",column = "address"),
            @Result(property = "line",column = "l_id",javaType = Line.class,
                    one=@One(select = "com.hongsheng.mapper.DriverMapper.findByIdLine"))
    })
    List<Driver> findAllDriver();
    /**
     * 通过线路id查线路
     * @param lid
     * @return
     */
    @Select("select * from tb_line where id=#{lid}")
    Line findByIdLine(@Param("l_id") int lid);


//    @Select("select * from tb_line where id=#{id}")
//    Line findLineByLId(@Param("id") Integer id);

    //根据id查询司机
    @Select("select * from tb_driver where id=#{id}")
    @Results({
            @Result(property = "driverName",column = "driver_name"),
            @Result(property = "driverPhone",column = "driver_phone"),
            @Result(property = "type",column = "type"),
            @Result(property = "carLoad",column = "car_load"),
            @Result(property = "size",column = "size"),
            @Result(property = "salary",column = "salary"),
            @Result(property = "status",column = "status"),
            @Result(property = "address",column = "address"),
            @Result(property = "line",column = "l_id",javaType = Line.class,
                    one=@One(select = "com.hongsheng.mapper.DriverMapper.findByIdLine"))
    })
    Driver findDriverById(int id);

    @Select("select * from tb_driver where status=0")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "driverName",column = "driver_name"),
            @Result(property = "driverPhone",column = "driver_phone"),
            @Result(property = "type",column = "type"),
            @Result(property = "carLoad",column = "car_load"),
            @Result(property = "size",column = "size"),
            @Result(property = "salary",column = "salary"),
            @Result(property = "status",column = "status"),
            @Result(property = "address",column = "address"),
            @Result(property = "line",column = "l_id",javaType = Line.class,
                    one=@One(select = "com.hongsheng.mapper.DriverMapper.findByIdLine"))
    })
    List<Driver> findFreeDriver();




    /**
     * 添加
     * @param driver
     * @return
     */
    @Insert("insert into tb_driver values(null,#{driverName},#{driverPhone},#{type},#{salary},#{status},null,#{carLoad},#{size},null)")
    Boolean addDriver(Driver driver);
    /**
     * 通过id修改
     * @param driver
     * @return
     */
    @Update("update tb_driver set driver_name=#{driverName},driver_phone=#{driverPhone},type=#{type},salary=#{salary},status=#{status},car_load=#{carLoad},size=#{size},address=#{address} where id=#{id}")
    Boolean updateDriver(Driver driver);
    @Update("update tb_driver set l_id=#{line.id} where id=#{id}")
    Integer setDriverStu(Driver driver);
    /**
     * 通过id删除
     * @param id
     * @return
     */
    @Delete("delete from tb_driver where id=#{id}")
    Boolean deleteDriver(Integer id);
    /**
     * 通过id查询
     * @param id
     * @return
     */
    @Select("select * from tb_driver where id=#{id}")
    Driver findAllById(Integer id);

    /**
     * 条件查询
     * @param driverName
     * @param type
     * @param status
     * @return
     */
    @SelectProvider(type = DriverSqlProvider.class,method = "getUsersByCondition")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "driverName",column = "driver_name"),
            @Result(property = "driverPhone",column = "driver_phone"),
            @Result(property = "type",column = "type"),
            @Result(property = "carLoad",column = "car_load"),
            @Result(property = "size",column = "size"),
            @Result(property = "salary",column = "salary"),
            @Result(property = "status",column = "status"),
            @Result(property = "address",column = "address"),
            @Result(property = "line",column = "l_id",javaType = Line.class,
                    one=@One(select = "com.hongsheng.mapper.DriverMapper.findByIdLine"))
    })
    List<Driver> getPageDriver(@Param("driverName") String driverName,
                               @Param("type") String type,
                               @Param("status") Integer status);


    /*
//实时更新
  @Select("SELECT CASE tr.order_status WHEN 0 THEN l.start_address WHEN " +
            "1 THEN l.start_address WHEN 2 THEN l.start_address WHEN 3 THEN '运输中' WHEN" +
            " 4 THEN l.end_address WHEN 5 THEN l.end_address END  AS address FROM tb_driver td LEFT JOIN tb_goods tg ON ( td.id = tg.dr_id ) " +
            "LEFT JOIN tb_order tr ON ( tr.id = tg.o_id ) LEFT JOIN tb_line l ON (l.id = td.l_id) where td.id=#{driverId} and tr.order_status=3 or tr.order_status=2 or tr.order_status=1 or tr.order_status=0   ")
    @ResultType(java.lang.String.class)
    String selectAddressByDriverId(Integer driverId);

//实时修改
    @Update("update tb_driver set address=#{address} where id=#{id}")
    Integer setAddressByDriver(@Param("address") String address, @Param("id") Integer id);

//查表中的线路id是否存在
    @Select("select l_id from tb_driver where id=#{id}")
    Integer findAllId(@Param("id") Integer id);*/

    @Select("select `status` from tb_driver where id=#{id}")
    Integer findStatus(Integer id);

    @Select("select driver_phone from tb_driver where driver_phone=#{driverPhone}")
    Boolean findPhone(String driverPhone);
}
